package util;

import hirondelle.date4j.DateTime;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Time {
	
	public static Timestamp Timestring2Timestamp(String tsStr){
		return String2Timestamp(tsStr,"yyyy-MM-dd HH:mm:ss");
	}

	public static Timestamp DateString2Timestamp(String tsStr){
		return String2Timestamp(tsStr,"yyyy-MM-dd");
	}
	public static Timestamp String2Timestamp(String tsStr,String format){
        //String tsStr = "2011-05-09 11:49:45";  
		SimpleDateFormat formatStyle = new SimpleDateFormat(format);
		Date logindate = null;
		try {
			logindate = formatStyle.parse(tsStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Timestamp timeStamp=new  Timestamp(logindate.getTime());
        return  timeStamp;
	}

	public static Timestamp CurrentTimestamp(){
		Timestamp currentTime = new Timestamp(System.currentTimeMillis());
		return currentTime;
	}
	
	public static String Timestamp2TimeString(Timestamp ts){
		return TimeStamp2String(ts,"yyyy-MM-dd HH:mm:ss");
	}

	public static String Timestamp2DateString(Timestamp ts){
		return TimeStamp2String(ts,"yyyy-MM-dd");
	}
	
	public static String Timestamp2YearString(){
		return TimeStamp2String(CurrentTimestamp(),"yyyy");
	}
	
	public static String Timestamp2YearString(Timestamp ts){
		return TimeStamp2String(ts,"yyyy");
	}
	
	public static String TimeStamp2String(Timestamp ts,String format){
		String tsStr = "";  
        DateFormat sdf = new SimpleDateFormat(format);  
        try {  
            tsStr = sdf.format(ts);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
		return tsStr;
	}
	
	//产生开始到结束的日期列表
	/*
	 * Timestamp begin:开始日期
	 * Timestamp end  :结束日期
	 * int day	:第几号
	 */
	public static List<Timestamp> dateBetweenTwoDaysList(Timestamp begin,Timestamp end,int day){
		DateTime begindate = new DateTime(Time.Timestamp2TimeString(begin));
		DateTime enddate = new DateTime(Time.Timestamp2TimeString(end));
		
		DateTime current20 = begindate.getStartOfMonth().plusDays(day-1);
		
		List<Timestamp> list = new ArrayList<Timestamp>();			
			
		list.add(Time.Timestring2Timestamp(begindate.toString()));
		if(!enddate.lt(current20)){
			if(begindate.lt(current20)){
				list.add(Time.Timestring2Timestamp(current20.toString()));
			}
			DateTime thisbegin=begindate;
			for(;;){
				thisbegin=thisbegin.getEndOfMonth().plusDays(1).getStartOfMonth().plusDays(day-1);
				if(enddate.lt(thisbegin)){
					break;
				}
				list.add(Time.Timestring2Timestamp(thisbegin.toString()));
			}
		}	
		list.add(Time.Timestring2Timestamp(enddate.toString()));
		
		return list;
	}
	
	//两个日期之间的天数
	public static int datediff(Timestamp begin,Timestamp end){
		DateTime begindate = new DateTime(Time.Timestamp2TimeString(begin));
		DateTime enddate = new DateTime(Time.Timestamp2TimeString(end));
		//日期长度
		return begindate.numDaysFrom(enddate);
	}
}

